package com.huawei.spare.part.replace.models;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.google.common.collect.Lists;
import com.huawei.spare.part.replace.models.constants.Plane;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Setter
@ToString
public class Switch implements Serializable {

    //  单板类型	交换平面	外部端口	是否有FC平面
    @Getter
    @Excel(name = "单板类型", isImportField = "true")
    String boardType;

    @Excel(name = "交换平面", isImportField = "true")
    String planes;

    @Getter
    @Excel(name = "外部端口", isImportField = "true")
    String externalPorts;

    @Getter
    @Excel(name = "是否有FC平面", isImportField = "true", replace = { "Yes_true", "No_false" })
    Boolean hasFcPlane;

//    @Excel(name = "所包含的交换软件版本", isImportField = "true")
//    String softwareVersions;
//
//    @Excel(name = "所包含的CPLD版本", isImportField = "true")
//    String cpldVersios;

    @Getter
    @Excel(name = "升级软件文件名关键字", isImportField = "true")
    String softwareKeyword;

    public List<Plane> getPlanes() {
        if (StringUtils.isNoneBlank(planes)) {
            final ArrayList<Plane> result = Lists.newArrayList();
            final String[] split = planes.split("/");
            for (String plane : split) {
                result.add(Plane.valueOf(plane));
            }
            return result;
        }
        return Lists.newArrayList();
    }

    public List<String> getPorts(int pos) {
        final ArrayList<String> ports = Lists.newArrayList();
        if (StringUtils.isNoneBlank(externalPorts)) {
            final ArrayList<String> result = Lists.newArrayList();
//            GE X/17/1--GE X/17/12
//            10GE X/20/1--10GE X/20/4
            final String[] planes = externalPorts.trim().split("\n");
            for (String plane : planes) {
                final String[] split = plane.trim().split("--");
                final String startIndex = StringUtils.substringAfterLast(split[0], "/");
                final String endIndex = StringUtils.substringAfterLast(split[1], "/");
                final String prefix = StringUtils.substringBeforeLast(split[0], "/");
                final String finalPrefix = prefix.replace(" X/", " "+ pos + "/");
                for (int index = Integer.parseInt(startIndex); index <= Integer.parseInt(endIndex); index++) {
                    ports.add(finalPrefix + "/" + index);
                }
            }
        }

        return ports;
    }

    public static void main(String[] args) {
        final Switch aSwitch = new Switch();
        aSwitch.setExternalPorts("10GE X/17/1--10GE X/17/8\n40GE X/17/1--40GE X/17/8");
        final List<String> ports = aSwitch.getPorts(3);
        System.out.println(ports);
    }

}
